package com.bank.utils;


import com.bank.entity.BaDeposit;
import com.bank.entity.BaRepay;
import com.bank.mapper.BaUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Component

public class ExcelUtils {
    @Autowired
    BaUserMapper  baUserMapper;
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class);
    public static  ExcelUtils excelUtils;
    @PostConstruct
    public void init(){
        excelUtils = this;
        excelUtils.baUserMapper = this.baUserMapper;
    }
    public static List<BaRepay> excelToShopIdList(InputStream inputStream) {
        List<BaRepay> list = new ArrayList<>();
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(inputStream);
            inputStream.close();
            //工作表对象
            Sheet sheet = workbook.getSheetAt(0);
            //总行数
            int rowLength = sheet.getLastRowNum() + 1;
            //工作表的列
            Row row = sheet.getRow(0);
            //总列数
            int colLength = row.getLastCellNum();
            //得到指定的单元格
            Cell cell ;
            for (int i = 1; i < rowLength; i++) {
                row = sheet.getRow(i);
                BaRepay baDeposit=new BaRepay();
                for (int j = 1; j < colLength; j++) {
                    cell = row.getCell(j);
                    if (cell != null) {
                        //将数字类型转化为String
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        Object data = cell.getStringCellValue();
                       if(j==1){
                           QueryWrapper wrapper=new QueryWrapper();
                           wrapper.eq("username",data);
                        baDeposit.setUserId(excelUtils.baUserMapper.selectOne(wrapper).getId());
                       }
                       if(j==2) {baDeposit.setPrice(Float.parseFloat(data.toString()));}
                       if(j==3) {
                          baDeposit.setRepayments(Float.parseFloat(data.toString()));
                       }
                       if(j==4){
                           baDeposit.setReTime(Integer.parseInt(data.toString()));
                       }
                       if(j==5){
                           baDeposit.setStatus(data.toString());
                       }
                    }
                }
                list.add(baDeposit);
            }
        } catch (Exception e) {
            LOGGER.error("parse excel file error :", e);
        }
        return list;
    }

}
